/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jason.web.tag.controller;

import com.jason.web.tag.entity.Category;
import java.util.List;
import javax.faces.component.UIComponent;
import org.primefaces.component.menubar.Menubar;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

/**
 *
 * @author jason
 */
public class BaseWebUtils {

    public static Menubar convertCategoryToMenubar(List<Category> categorys) {
        Menubar menubara = new Menubar();
        menubara = (Menubar) convertCategoryToMenubar(menubara, categorys);
        return menubara;
    }

    private static UIComponent convertCategoryToMenubar(UIComponent ui, List<Category> categorys) {
        for (Category c : categorys) {
            if (c.getDisabled()) {//TODO: primession
                continue;
            }
            if (c.getCategoryList().isEmpty()) {
                MenuItem menuItem = new MenuItem();
                menuItem.setAjax(c.getAjax());
                menuItem.setAsync(c.getAsync());
                //menuItem.setDisabled(c.getDisabled());
                menuItem.setIncludeViewParams(c.getIncludeViewParams());
                menuItem.setPartialSubmit(c.getPartialSubmit());
                menuItem.setGlobal(c.getGlobal());
                menuItem.setUrl(c.getUrl());
                menuItem.setFragment(c.getFragment());
                menuItem.setIcon(c.getIcon());
                menuItem.setOnclick(c.getOnclick());
                menuItem.setOncomplete(c.getOncomplete());
                menuItem.setOnerror(c.getOnerror());
                menuItem.setOnstart(c.getOnstart());
                menuItem.setOnsuccess(c.getOnsuccess());
                menuItem.setProcess(c.getProcess());
                menuItem.setStyle(c.getStyle());
                menuItem.setStyleClass(c.getStyleClass());
                menuItem.setTarget(c.getTarget());
                menuItem.setTitle(c.getTitle());
                menuItem.setUpdate(c.getUpdateRender());
                menuItem.setValue(c.getName());

                ui.getChildren().add(menuItem);
            } else {
                Submenu submenu = new Submenu();
                submenu.setLabel(c.getName());
                submenu.setIcon(c.getIcon());
                submenu.setStyle(c.getStyle());
                submenu.setStyleClass(c.getStyleClass());
                ui.getChildren().add(submenu);
                convertCategoryToMenubar(submenu, c.getCategoryList());
            }
        }
        return ui;
    }

    public static TreeNode convertCategorysToTreeNode(TreeNode tree, List<Category> cs) {
        for (Category c : cs) {
            if (c.getDisabled()) {//TODO: primession
                continue;
            }
            DefaultTreeNode node = new DefaultTreeNode(c, tree);
            if (!c.getCategoryList().isEmpty()) {
                convertCategorysToTreeNode(node, c.getCategoryList());
            }
        }

        return tree;
    }

    private static TreeNode convertCategorysToTreeNode(List<Category> categorys) {
        TreeNode tree = new DefaultTreeNode("ROOT", null);
        for (Category c : categorys) {
            if (c.getDisabled()) {//TODO: primession
                continue;
            }
            TreeNode node = new DefaultTreeNode(c, tree);

        }
        return tree;
    }
}
